﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Dotnet.FormExtensions
{
    public static partial  class Ext
    {
        /// <summary>
        /// 获取窗口所有控件
        /// </summary>
        /// <param name="control"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static void GetFormAllControls(this Control control,List<Control> list)
        {
            Control.ControlCollection children = control.Controls;
            foreach (Control item in children)
            {
                if (item.Controls != null)
                {
                    GetFormAllControls(item,list);
                }
                else
                {
                    list.Add(item);
                }
            }
        }
        public static T Clone<T>(this T controlToClone) where T : Control
        {
            PropertyInfo[] controlProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            T instance = Activator.CreateInstance<T>();

            foreach (PropertyInfo propInfo in controlProperties)
            {
                if (propInfo.CanWrite)
                {
                    if (propInfo.Name != "WindowTarget")
                        propInfo.SetValue(instance, propInfo.GetValue(controlToClone, null), null);
                }
            }
            return instance;
        }
    }
}
